// IPDialog.cpp : implementation file
//

#include "stdafx.h"
#include "rmstranslator.h"
#include "IPDialog.h"

#include "../RMSLib/utility.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CIPDialog dialog


CIPDialog::CIPDialog(char* Title,int ip,int Port,CWnd* pParent /*=NULL*/)
	: CDialog(CIPDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CIPDialog)
	m_Port = 0;
	//}}AFX_DATA_INIT
	m_title=Title;
	m_Port=Port;
	IP=ip;

}


void CIPDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIPDialog)
	DDX_Control(pDX, IDC_IP_ADDRESS, m_IP);
	if (m_Port) DDX_Text(pDX, IDC_PORT, m_Port);
	if (m_Port) DDV_MinMaxUInt(pDX, m_Port, 1, 65535);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CIPDialog, CDialog)
	//{{AFX_MSG_MAP(CIPDialog)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIPDialog message handlers

BOOL CIPDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	SetWindowText(m_title);


	if (m_Port==0) {
		// disable "port"
		GetDlgItem(IDC_PORT)->EnableWindow(FALSE);
	}

	// Fill in 
	int i=0,sel=0;

	while (i<100) {
		int r,ip=GetLocalHostIP(i++);
		if (ip==0) break;
		if (ip) r=m_IP.AddString(GetIPStr(ip));
		if (ip==IP) sel=r;
	}
	m_IP.SetCurSel(sel);

	UpdateData(FALSE);	

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CIPDialog::OnOK() 
{
	// TODO: Add extra validation here
	CString SelectedIP;
	int i=0;
	
	CDialog::OnOK();

	UpdateData(TRUE);

	// translete selction number back to IP
	m_IP.GetLBText(m_IP.GetCurSel(), SelectedIP);
	while (i<100) {
		int ip=GetLocalHostIP(i++);
		if (ip==0) break;
		if (strcmp(GetIPStr(ip),SelectedIP)==0) {
			IP=ip;
			break;
		}
	}

}
